﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public static string currentLang;
        public string LoginName { get; set; }
        public Login()
        {
            InitializeComponent();
            //默认是中文
            currentLang = "/Common/Language/zh_CN";
            Variable.LanguageSeesion = currentLang;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");

        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void btn_login_Click(object sender, RoutedEventArgs e)
        {
            string name = this.txtName.Text;
            string pass = this.txtPass.Password;

            if (string.IsNullOrWhiteSpace(name))
            {
                MessageBox.Show("请输入登录名");
                return;
            }
            if (string.IsNullOrWhiteSpace(pass))
            {
                MessageBox.Show("请输入登录密码");
                return;
            }

            int count = DBHelper.GetCount("select t.* from t_manager t where t.name = @name and t.password = @password", new SqlParameter[] { new SqlParameter("@name", name), new SqlParameter("@password", pass) });
            if (count > 0)
            {
                this.LoginName = name;
                this.DialogResult = true;

                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                cfa.AppSettings.Settings["LoginName"].Value = this.LoginName;
                cfa.AppSettings.Settings["LoginPassword"].Value = "";
                cfa.AppSettings.Settings["LoginTips"].Value = "";
                cfa.Save();
            }
            else
            {
                MessageBox.Show("登录名或密码不正确");
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtName.Text = ConfigurationManager.AppSettings["LoginName"];
            this.txtPass.Password = ConfigurationManager.AppSettings["LoginPassword"];
            this.lbl_tips.Content = ConfigurationManager.AppSettings["LoginTips"];
            if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["LoginTips"]))
            {
                this.lbl_tips.Visibility = Visibility.Collapsed;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (this.DialogResult != true)
            {
                Environment.Exit(0);
            }
        }
        private MainWindow MainWindow;
        private void cbxlanguage_DropDownClosed(object sender, EventArgs e)
        {

            {
                string language = this.cbxlanguage.Text;
                string languageType = "";
                if (!string.IsNullOrEmpty(language))
                {
                    switch (language)
                    {
                        case "中文": languageType = "/Common/Language/zh_CN"; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN"); break;
                        case "英文": languageType = "/Common/Language/en_US"; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en"); break;
                        case "Chinese": languageType = "/Common/Language/zh_CN"; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN"); break;
                        case "English": languageType = "/Common/Language/en_US"; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en"); break;
                    }

                    ChangeLanguage.change(languageType, this);
                    if (MainWindow != null)
                    {
                        ChangeLanguage.change(languageType, MainWindow);
                    }
                    currentLang = languageType;
                    Variable.LanguageSeesion = languageType;
                }
            }
        }
    }
}
